//
//  ZZEmotionPageViewCell.swift
//  web
//
//  Created by 张卓 on 2017/2/10.
//  Copyright © 2017年 ZZ. All rights reserved.
//

import UIKit

class ZZEmotionPageViewCell: UICollectionViewCell {
    //定义一个数组保存按钮
    var emoticonButtonArr:[ZZEmoticonButton] = [ZZEmoticonButton]()
    
    // indexPath
    var indexPath: IndexPath? {
        didSet{
//            messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
        }
    }
    
    //定义一个属性共外界赋值(每一页通过一个一位数组完成赋值)
    var emoticons:[ZZEmoticonModel]?{
        didSet{
            //把 20 个按钮全部隐藏
            for button in emoticonButtonArr{
                button.isHidden = true
            }
            
            //遍历模型数组(0,7)
            for (i,emoticonModel) in emoticons!.enumerated(){
                let button = emoticonButtonArr[i]
                //显示button
                button.isHidden = false
                //给button赋值
                button.emoticonModel = emoticonModel
                //判断emoji还是图片表情
                if emoticonModel.isEmoji {
                    let code = ((emoticonModel.code ?? "") as NSString).emoji()
                    //设置title
                    button.setTitle(code, for: UIControlState.normal)
                    //image 设置为nil
                    button.setImage(nil, for: UIControlState.normal)
                }else{
                    //图片表情
                    let path = emoticonModel.path ?? ""
                    //获取bundle文件中图片
                    let image = UIImage(named: path, in: ZZEmoticonTools.shared.emoticonBundle, compatibleWith: nil)
                    
                    button.setImage(image, for: UIControlState.normal)
                    button.setTitle(nil, for: UIControlState.normal)
                    
                }
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置20个按钮的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        //计算按钮的宽和高
//        let buttonW = self.frame.size.width / CGFloat(ZZEMOTICONMAXCOL)
//        let buttonH = self.frame.size.height / CGFloat(ZZEMOTICONMAXROW)
        let buttonW = (ZZSCREENW - 10) / CGFloat(ZZEMOTICONMAXCOL)
        let buttonH = (216 - 35 - 20) / CGFloat(ZZEMOTICONMAXROW)
        
        for (i,button) in emoticonButtonArr.enumerated() {
            //列索引
            let colIndex = CGFloat(i % ZZEMOTICONMAXCOL)
            //行索引
            let rowIndex = CGFloat(i / ZZEMOTICONMAXCOL)
            //设置frame
            button.frame = CGRect(x: 5 + colIndex * buttonW, y: rowIndex * buttonH, width: buttonW, height: buttonH)
            
        }
        //设置删除按钮的frame
        deleteButton.frame = CGRect(x: ZZSCREENW - buttonW - 5, y: buttonH * 2, width: buttonW, height: buttonH)
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = ZZRandomColor()
        addChildButtons()
        //添加控件

        contentView.addSubview(deleteButton)
    }
    //MARK: - 监听点击事件
    @objc private func  buttonClick(button: ZZEmoticonButton){
        //发送通知 -> 传出按钮对应的模型
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: EMOTICONBUTTONNOTI), object: button.emoticonModel)
    }
    
    @objc private func deleteButtonClick(){
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: EMOTICONDELETEBUTTONNOTI), object: nil)
    }
    
    
    
    //MARK: - 添加20 个按钮
    private func addChildButtons(){
        for _ in 0..<ZZEMOTICONMAXCOUNT {
            //创建按钮
//            let button = UIButton()
            let button = ZZEmoticonButton()
            //监听事件
            button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
            //设置随即颜色
//            button.backgroundColor = ZZRandomColor()
            //设置字体
            button.titleLabel?.font = UIFont.systemFont(ofSize: 30)
            //添加
            emoticonButtonArr.append(button)
            contentView.addSubview(button)
        }
    }
    
    
    
    //MARK: - 懒加载控件
//    private lazy var messageLabel: UILabel = {
//        let lab = UILabel()
//        lab.font = UIFont.systemFont(ofSize: 35)
//        lab.numberOfLines = 0
//        return lab
//    }()
    //删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(deleteButtonClick), for: UIControlEvents.touchUpInside)
        button.setImage(UIImage(named:"compose_emotion_delete"), for: UIControlState.normal)
        button.setImage(UIImage(named:"compose_emotion_delete_highlighted"), for: UIControlState.highlighted)
        return button
    }()
    
}
